package cc.ufinity.networkdiagnose.view;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.manager.MessageWhat;
import cc.ufinity.networkdiagnose.manager.TelManager;
import cc.ufinity.networkdiagnose.manager.WlanManager;
import cc.ufinity.networkdiagnose.test.Location;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

public class EnvironmentActivity extends Activity{
	private int gpsOrNet = LocationClientOption.GpsFirst;
	public static LocationClient mLocClient;
	//private LinearLayout phoneLayout;
	private WlanManager wlanManager;
	private TextView addressText;
	private ListView cmccList;
	private TextView netWorkDate;
	private Handler handler;
	private TelManager tm;
	private SimpleAdapter adapter;
	private TextView headTitle;
	private Button btback;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.environmentalinformation);
		addressText=(TextView)findViewById(R.id.geographicAddress);
		cmccList=(ListView)findViewById(R.id.cmccList);
		netWorkDate=(TextView)findViewById(R.id.networkDate);
		wlanManager = new WlanManager(EnvironmentActivity.this);
		
		btback = (Button)findViewById(R.id.btn_httpcfg_back);
		btback.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				EnvironmentActivity.this.finish();
			}
			
		});
		
		headTitle=(TextView)findViewById(R.id.head_title33);
		//phoneLayout = (LinearLayout)findViewById(R.id.networkDate);
		
		handler=new Handler(){

			public void handleMessage(Message msg) {
				switch(msg.what){
				case 1:
					addressText.setText("经度 ："+GlobalApp.longitude+"\n纬度  :  "+GlobalApp.latitude);
					try{
						if(mLocClient.isStarted()){
							mLocClient.stop();
						}
					}catch(Exception e){}
					
					break;
				case MessageWhat.WIFI_RECEIVE_LIST:
					updateWifi();
					break;
				case MessageWhat.TEL_STATE_SUCCESS:
					updateTel();    
					break;
				}
			}
		};
		
		if(GlobalApp.longitude.equals("0.0")&&GlobalApp.latitude.equals("0.0")){
			Location loc=new Location(getApplicationContext(),handler);
			mLocClient=loc.mLocationClient;
			setLocationOption();
			mLocClient.start();
		}else{
			addressText.setText("经度 ："+GlobalApp.longitude+"\n纬度  :  "+GlobalApp.latitude);
		}
		
		
		
		tm = new TelManager(this);
		tm.setHandler(handler);
		
		wlanManager.setHandler(handler);
		start();
	}
	
	private void updateTel() {
		if(tm.getNetworkType().equals("EDGE") || tm.getNetworkType().equals("GPRS")){
			headTitle.setText("GSM网络");
		} else if(tm.getNetworkType().equals("HSDPA") || tm.getNetworkType().equals("HSUPA")){
			headTitle.setText("TD网络");
		}
		netWorkDate.setText("网络类型: " + tm.getNetworkType()+
				"\n信号强度: " + tm.getStrSignalStrength()+" dBm"
				+"\nLAC: " + tm.getLac() 
				+"\nCI: " + tm.getCI());
	}

	private void updateWifi() {
		List<Map<String, Object>> data = wlanManager.getWifiList(); 
		if(data.size() > 0) {
			adapter = null;
			 adapter = new SimpleAdapter(this, data,
					R.layout.rssi_list_widget, new String[] { "ssid", "mac", "img",
							"level","xindao"}, new int[] { R.id.wifi_ssid, R.id.wifi_mac,
							R.id.wifi_img, R.id.wifi_level ,R.id.wifi_xindao});
			cmccList.setAdapter(adapter);
		}
		
	}
	
	/**
	 * 开始扫描周围的AP
	 */
	private void start() {
		new Thread(){
			public void run(){
				wlanManager.startScan();
			}
		}.start();
	}
	
	
	
	/**
	 * 获取的是经纬度所需要的参数的设置
	 */
	private void setLocationOption(){
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);			 
		option.setCoorType("bd09ll");		 
		option.setServiceName("com.baidu.location.service_v2.9");
		option.setScanSpan(2000); 
		option.setAddrType("all");  
		option.setPriority(gpsOrNet);        
//		option.setPoiNumber(10);
		option.disableCache(true);		
		mLocClient.setLocOption(option);
	}
	 

}
